/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;


/**
 *
 * @author THANH PHUC
 */
public class JumpManager extends LayerManager{

    /**
    * The width of the display window.
    */
    static int DISP_WIDTH;
    static int DISP_HEIGHT;
    static int CANVAS_X;
    static int CANVAS_Y;
    int Square =16;
     PlayerBom myPlayer;
    private MapBom map;
    public  TiledLayer  background;
    public Image backgroundImg;
    public Gach [] CacVienGach;
    public Monster[] _ListMonster;
    //private boolean myLeft;
    //private boolean myUp;
    private int myAction = -1;
    
    //1: Phai
    //2: Trai
    //3: Len
    //4: Xuong
    private int myCurrentLeftX;
    public int []  VienGach;
    int SoCot = 15;
    int SoDong = 18;
    //Monster
     
     //Bom
    int MaxNumberBom = 5;
     int CuongDo = 4;
     int MaxCuongDo = 6;
     private Bomb[] _ListBom;
     Bomb b;
     public int avaibleBom;
      int[][] cells = {
      {0, 0, 1, 2, 2, 2, 2, 2, 3, 4, 3, 3, 1,2,4,3}, // sky
      {2, 2, 2, 2, 2, 3, 3 ,4 ,3 ,2, 3, 4, 2,2,4,3}, // sky
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 1,2,4,3},// earth
      {0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0,  0, 0, 1,2,4,3},// sea
      {0, 0, 0, 0, 5 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
      {2, 0, 0, 0, 0 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
      {0, 0, 0, 0, 0 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
      {2, 0, 0, 0, 2 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
    {1, 0, 0, 0, 0 ,1 ,0, 0, 2, 0, 0, 0, 1,2,4,3},
    {0, 0, 0, 5, 0 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
    {0, 0, 0, 0, 0 ,1 ,0, 0, 1, 0, 0, 0, 1,2,4,3},
    {1, 0, 0, 0, 0 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
    {0, 0, 0, 0, 0 ,2 ,0, 0, 3, 0, 0, 0, 1,2,4,3},
    {0, 0, 0, 0, 0 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
    {0, 0, 0, 0, 0 ,0 ,0, 0, 1, 0, 0, 0, 1,2,4,3},
    {1, 0, 0, 0, 0 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
    {1, 0, 0, 0, 0 ,0 ,0, 0, 0, 0, 0, 0, 1,2,4,3},
    {1, 0, 0, 1, 1 ,1 ,1, 0, 0, 0, 0, 0, 1,2,4,3}
    
  };
      public int [][]cellsClone;
  
    void setAction(int action)
    {
        
        myAction = action;
    }

    public JumpManager(int x, int y, int width, int height)throws Exception
    { 
         CANVAS_X = x;
         CANVAS_Y = y;
         DISP_WIDTH = width;
         DISP_HEIGHT = height;
         
         //myCurrentLeftX = 100;
         myCurrentLeftX = 0;
         setViewWindow(0, 0,width,height );
         /*
         if(myCowboy == null) {
          myCowboy = new Cowboys();
          append(myCowboy);
         */
         
         if(myPlayer == null)
         {
             myPlayer = new PlayerBom(0, 0,this);
             append(myPlayer);
             avaibleBom = myPlayer.NumBom;
         }
          if(_ListMonster == null)
         {
              _ListMonster = new Monster[3];
               
              for (int i = 0 ; i < _ListMonster.length; i ++)
              {
                  int index = Monster.GetRandomInt()%3;
                  if (index ==0)
                      index =1;
                  String Imagine_Monster="/images/Monster"+ String.valueOf(index) +".png";
                  _ListMonster[i] = new Monster(50, 100,Imagine_Monster);
                  append(_ListMonster[i]);
                   
              }
         
         }
          if (_ListBom ==null)
        {
            _ListBom = new Bomb[MaxNumberBom];
            
        }
         if (map == null)
         {
              createBackground();
             map = new MapBom();
              append(map);
             
            // insert(map, this.getSize());
         }
         cellsClone = new int [cells.length][cells[1].length];
       for (int i = 0 ; i < cells.length; i ++)
       {
           for (int j = 0; j < cells[1].length;j ++)
           {
               if (cells[i][j]>2)
                   cellsClone[i][j] =2;
               else
                   cellsClone[i][j] = cells[i][j];
           }
       }
         
    }
    int advance(int gameTicks) {
    int retVal = 0;
  
    myPlayer.advance(gameTicks, myAction);
    if (myPlayer.AliveCount==0)
        return 0;
    
    int i;
     
      for (  i = 0 ; i < _ListBom.length; i ++)   
        if(_ListBom[i] !=null)
        {
             if (_ListBom[i].BomNo ==false)
             {  
                 _ListBom[i].advance(CuongDo, this);
                 
             }
             else
             {
                    _ListBom[i].RemoveNhanh(this);
                   remove(_ListBom[i]);
                   
                    _ListBom[i] = null;
                    avaibleBom++;
                 
             }
              
        }
    
     for (i = 0; i < _ListMonster.length; i ++)
     {
         
         Monster temp_monster = _ListMonster[i];
         if (temp_monster ==null)
             continue;
         _ListMonster[i].advance();
             if (_ListMonster[i].collidesWith(background,false))
             {
                 _ListMonster[i].GetBack();
                
            }
        if (_ListMonster[i].collidesWith(myPlayer, true))
        {
            myPlayer.IsAlive = false;
            
        }
     
    
     }
      
     boolean vacham = myPlayer.collidesWith(background,  false);
            if (vacham && myPlayer.IsAlive ==true)
            {
             //   setPosition(preX, preY);
               // jump_manager.background.setVisible(false);
                int ToaDox = myPlayer.getX()+5;
                int ToaDoy = myPlayer.getY()+15;
                int row = ToaDoy/Square;
                int colum = (ToaDox)/Square;
                boolean checkODacBiet = false;
                while (ToaDoy < myPlayer.getY()+myPlayer.getHeight() )
                {
                    if (cellsClone[row][colum]>2)
                    {
                        switch(cellsClone[row][colum])
                        {
                            case 3: //Tang Speed
                                myPlayer.speed ++;
                                if (myPlayer.speed >6)
                                    myPlayer.speed = 6;
                                break;
                            case 4: //Tang Do dai bom
                                CuongDo ++;
                                if (CuongDo > MaxCuongDo)
                                    CuongDo = MaxCuongDo;
                                break;
                            case 5: //Tang So Luong Bom (So Luong Bom van fai nho hon
                                //so luong  bom toi da
                                avaibleBom ++;
                                if (avaibleBom > MaxNumberBom)
                                    avaibleBom = MaxNumberBom;
                                break;
                                //if (avaibleBom )
                        }
                        background.setCell(colum, row, 0);
                        checkODacBiet = true;
                                
                        break;
                    }
                    ToaDoy +=10;
                    row = ToaDoy/Square;
                }
                if (checkODacBiet==false)
                    {
                        myPlayer.GoBack(myAction);

                     if (myAction != 4)
                        myPlayer.move(0, 1);
                    }
                    
            }
    
    myAction = -1;
       return(retVal);
    }
    

      public void paint(Graphics g) 
      {
        setViewWindow(myCurrentLeftX, 0, DISP_WIDTH, DISP_HEIGHT);
        paint(g, CANVAS_X, CANVAS_Y);
      }
      private void createBackground() throws Exception {

  // load the image
   backgroundImg = Image.createImage("/images/background.png");

  // create the tiledlayer background
  background = new TiledLayer(SoCot, SoDong, backgroundImg, 16, 16);

  // array that specifies what image goes where
 

  // set the background with the images
  CacVienGach = new Gach[50];
  int dem =0;
  for (int i = 0; i < SoDong; i++) 
  {
      for (int j = 0; j < SoCot;j ++)
          if (cells[i][j] >1)
          background.setCell(j, i, 2);
          else
              background.setCell(j, i, cells[i][j]);
      
 // background.setPosition(DISP_WIDTH/2, DISP_HEIGHT/2);
  }
   
 /* for (int i =0; i < 10; i ++)
  {
      background.setCell(0, i, 1);
  }
  */
  append(background);
  // set the location of the background
  //background.setPosition(GAME_ORIGIN_X, GAME_ORIGIN_Y);
  
}
      /*
    void AppendBomb()
    {
        if(b == null)
        {
          try {
            b = new Bomb(myPlayer.getX(), myPlayer.getY());
             //append(b);
            insert(b, getSize()-1);
          } catch (Exception exception) {
          }
           
        }
  
        
    }*/
        
      
      void AppendBomb()
    {
          if (myPlayer.IsAlive==false)
              return;
         
        if (avaibleBom <1 || avaibleBom >MaxNumberBom)
            return;
        try
        {
            for (int i = 0 ; i < _ListBom.length; i ++)
            {
                if (_ListBom[i]==null)
                {
                   // _ListBom[i].move(myPlayer.getX(),  myPlayer.getY()+10);
                    //_ListBom[i] = new Bomb(myPlayer.getX(), myPlayer.getY()+10,this);
                       int ToaDox = myPlayer.getX()+5;
                       int ToaDoy = myPlayer.getY()+15;
                       int row = ToaDoy/Square;
                       int colum = (ToaDox)/Square;
              
                    _ListBom[i] = new Bomb(colum*Square, row*Square);
                    insert(_ListBom[i],getSize()-1);
                    //append(_ListBom[i]);
                    avaibleBom --;
                    break;
                }
            }
        }
        catch(Exception ex)
        {
            throw null;
        }
                
    }
      public void RemoveLayer(Layer layer)
      {
         remove(layer); 
      }
      public void DeleteAllComponent()
      {
          myPlayer = null;
          int i;
          for (i = 0 ; i < _ListBom.length; i ++)
              if (_ListBom[i] !=null)
                  _ListBom[i] = null;
          _ListBom = null;
          for (i = 0 ; i < _ListMonster.length ; i ++)
              if (_ListMonster[i]!=null)
                  _ListMonster[i] = null;
          _ListMonster = null;
          map = null;
          background = null;
      }
}
